﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace FastAccess
{
    public partial class ProxyBoxForm : Form
    {
        public ProxyBoxForm()
        {
            InitializeComponent();
        }

        private void ProxyBoxForm_Load(object sender, EventArgs e)
        {
            //LoadData();
        }

        public ProxyDB Db
        {
            get
            {
                return proxyDB;
            }
        }

        public void LoadData()
        {
            string Path = Settings.WorkingDir + Settings.ProxyListFileName;
            if (!File.Exists(Path))
            {
                using (StreamWriter sw = File.CreateText(Path))
                {
                    sw.Write(" ");
                    sw.Flush();
                    sw.Close();
                }
            }
            else
            {
                proxyDB.ReadXml(Path, XmlReadMode.ReadSchema);
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            lbState.Text = "开始保存";
            saveWorker.RunWorkerAsync();
        }

        private void Save(object sender, DoWorkEventArgs e)
        {
            string Path = Settings.WorkingDir + Settings.ProxyListFileName;
            proxyDB.WriteXml(Path, XmlWriteMode.WriteSchema);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog(this);
        }

        private void Saveok(object sender, RunWorkerCompletedEventArgs e)
        {
            lbState.Text = "保存完毕";
        }

        private void importProxyFile(object sender, DoWorkEventArgs e)
        {
            string Path = e.Argument.ToString();
            string[] splits = null;
            string IP = null;
            string port = null;
            string ipport = null;
            using (StreamReader rd = new StreamReader(Path, Encoding.Default))
            {
                int count = 0;
                string Line = null;
                while ((Line = rd.ReadLine()) != null)
                {
                    if (Line.Length > 15)
                    {
                        Line = Line.Replace("\t", " ");
                        splits = Line.Split(' ');
                        if (splits.Length > 1)
                        {
                            ipport = splits[0];
                            splits = ipport.Split(':');
                            if (splits.Length > 1)
                            {
                                IP = splits[0];
                                port = splits[1];
                                ProxyDB.ProxyLsRow row = proxyDB.ProxyLs.NewProxyLsRow();
                                row.IP = IP;
                                row.Port = port;
                                row.UserName = "Anymouse";
                                row.Password = "N/A";
                                if (Helper.AccessByProxy(row, Settings.TestUrl))
                                {
                                    try
                                    {
                                        proxyDB.ProxyLs.AddProxyLsRow(row);
                                        count++;
                                        importWorker.ReportProgress(count);
                                    }
                                    catch (System.Data.ConstraintException ex)
                                    {
                                    }
                                }                                
                            }
                        }
                    }
                }
            }
            importWorker.ReportProgress(100);
        }


        private void ReportStatus(object sender, ProgressChangedEventArgs e)
        {
            workProgress.Value = e.ProgressPercentage;
        }

        private void ImportWorkDone(object sender, RunWorkerCompletedEventArgs e)
        {
            lbState.Text = "导入完毕";
        }

        private void StartImport(object sender, CancelEventArgs e)
        {
            if (!importWorker.IsBusy)
            {
                lbState.Text = "开始导入...";
                importWorker.RunWorkerAsync(openFileDialog1.FileName);
            }
            else
            {
                lbState.Text = "导入执行中...";
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ProxyAddForm addf = new ProxyAddForm();
            addf.Db = proxyDB;
            addf.ShowDialog(this);
        }
    }
}
